﻿/**
 * Copyright (c) 2008 Justin Etheredge http://www.codethinked.com
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Microsoft Public License (Ms-PL)
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/ms-pl.html
 * If redistributing this code, this entire header must remain intact.
 * 
 * Some of the code in this application has been taken 
 * from the following sources (all of which are licensed under
 * the Ms-PL):
 * Matt Warren's Blog: http://blogs.msdn.com/mattwar/
 * MSDN: http://msdn2.microsoft.com/en-us/library/bb546158.aspx
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace LinqToSimpleDB
{
  internal static class SimpleDBNumericHelper
  {
    public static string PadAndFormatNumber(Int32 num, int precision, int scale)
    {
      return PadAndFormatNumber(num.ToString(CultureInfo.InvariantCulture), precision, scale);
    }

    public static string PadAndFormatNumber(Int64 num, int precision, int scale)
    {
      return PadAndFormatNumber(num.ToString(CultureInfo.InvariantCulture), precision, scale);
    }

    public static string PadAndFormatNumber(UInt32 num, int precision, int scale)
    {
      return PadAndFormatNumber(num.ToString(CultureInfo.InvariantCulture), precision, scale);
    }

    public static string PadAndFormatNumber(UInt64 num, int precision, int scale)
    {
      return PadAndFormatNumber(num.ToString(CultureInfo.InvariantCulture), precision, scale);
    }

    public static string PadAndFormatNumber(double num, int precision, int scale)
    {
      return PadAndFormatNumber(num.ToString(CultureInfo.InvariantCulture), precision, scale);
    }

    public static string PadAndFormatNumber(Decimal num, int precision, int scale)
    {
      return PadAndFormatNumber(num.ToString(CultureInfo.InvariantCulture), precision, scale);
    }

    private static int WholeNumberPaddingCharacters(int precision, int scale)
    {
      return precision - scale;
    }

    private static int FractionalPaddingCharacters(int precision, int scale)
    {
      return scale;
    }

    private static string PadAndFormatNumber(string num, int precision, int scale)
    {
      string result = string.Empty;
      string[] parts = num.Split(Char.Parse(CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator));
      
      int wholePlaces = WholeNumberPaddingCharacters(precision, scale);
      if (parts[0].Length > wholePlaces)
      {
        throw new Exceptions.InvalidNumberException("Number had too many whole number places.");
      }
      result = parts[0].PadLeft(wholePlaces, '0');
      result += ".";
      int fractionalPlaces = FractionalPaddingCharacters(precision, scale);
      if (parts.Length > 1)
      {
        if (parts[1].Length < fractionalPlaces)
        {
          result += parts[1].PadRight(fractionalPlaces, '0');
        }
        else
        {
          result += parts[1].Substring(0, fractionalPlaces);
        }
      }
      else
      {
        result += string.Empty.PadRight(fractionalPlaces, '0');
      }
      return result;
    }
  }
}
